//
//  setVC.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/21.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "setVC.h"
#import "LogOutCell.h"
#import "changePasswordVC.h"
#import "changeTradePasswordVC.h"
#import "realNameVC.h"
#import "CRChelpCenterController.h"
#import "aboutUsVC.h"
#import "resultVC.h"
#import "certificationCompleteVC.h"
@interface setVC ()  <UITableViewDelegate, UITableViewDataSource> {
    NSString *Certification;
    NSString *realName;
    NSString *cardId;
}
@property (nonatomic, strong) UITableView *detialView;
@end

@implementation setVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self isCertificationRequest];
    Certification = @"未认证";
    self.title = @"设置";
    CGRect frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - TabbarSafeBottomMargin);
    self.detialView = [[UITableView alloc]initWithFrame:frame];
    self.detialView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.detialView.tableFooterView = [UIView new];
    if (@available(iOS 11.0, *)) {
        self.detialView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        //        if (iPhoneX) {
        //            self.detialView.contentInset = UIEdgeInsetsMake(0, 0, 88, 0);
        //        }else {
        //            self.detialView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
        //        }
        //        self.detialView.scrollIndicatorInsets = self.detialView.contentInset;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.detialView.delegate = self;
    self.detialView.dataSource = self;
    [self.detialView registerNib:[UINib nibWithNibName:NSStringFromClass([LogOutCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LogOutCell class])];
    self.detialView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [self.view addSubview:self.detialView];
}

#pragma mark --- uitableViewDelegate and dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0 || section == 2) {
        return 1;
    }
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 2) {
        return 65;
    }
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:NSStringFromClass([UITableViewCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.font = [UIFont customFontOfSize:13];
        cell.detailTextLabel.font = [UIFont customFontOfSize:13];
    }
    if (indexPath.section == 0) {
        cell.textLabel.text = @"实名认证";
        cell.detailTextLabel.text = Certification;
        return cell;
    }else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"修改登录密码";
        }else if (indexPath.row == 1) {
            cell.textLabel.text = @"修改交易密码";
        }else if (indexPath.row == 2) {
            cell.textLabel.text = @"关于我们";
        }else {
            cell.textLabel.text = @"帮助中心";
        }
        return cell;
    }
    LogOutCell *logOuCcell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LogOutCell class])];
    logOuCcell.separatorInset = UIEdgeInsetsMake(0, 0, 0, cell.bounds.size.width);
    return logOuCcell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            changePasswordVC *VC = [changePasswordVC new];
            [self.navigationController pushViewController:VC animated:YES];
        }else if (indexPath.row == 1) {
            changeTradePasswordVC *VC = [changeTradePasswordVC new];
            [self.navigationController pushViewController:VC animated:YES];
        }else if (indexPath.row == 3) {
            CRChelpCenterController *helpVC = [[CRChelpCenterController alloc]init];
            [self.navigationController pushViewController:helpVC animated:YES];
        }else if (indexPath.row == 2) {
            aboutUsVC *VC = [aboutUsVC new];
            [self.navigationController pushViewController:VC animated:YES];
        }
    }else if (indexPath.section == 0) {
        if ([Certification isEqualToString:@"已认证"]) {
            certificationCompleteVC *VC = [[certificationCompleteVC alloc]initWithNibName:@"certificationCompleteVC" bundle:nil];
            VC.info = @[realName, cardId];
            [self.navigationController pushViewController:VC animated:YES];
        }else {
            realNameVC *VC = [realNameVC new];
            [self.navigationController pushViewController:VC animated:YES];
        }
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *grayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 20)];
    grayView.backgroundColor = [UIColor clearColor];
    return grayView;
}
//请求是否认证
- (void)isCertificationRequest {
    NSDictionary *dic = @{
                          @"token":USERTOKEN,
                          };
    NSString *url = [NSString stringWithFormat:@"%@", [[apiManger shareApiManger] isShiMingRenZheng]];
    __weak typeof(self)ws = self;
    [networkManger PostHttpDataWithUrlStr:url Dic:dic SuccessBlock:^(id responseObject) {
        __strong typeof(ws)ss = ws;
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            NSString *status = [NSString stringWithFormat:@"%@", responseObject[@"status"]];
            if ([status isEqualToString:@"1"]) {
                ss -> Certification = @"已认证";
                ss -> realName = responseObject[@"realName"];
                ss -> cardId = responseObject[@"cardId"];
                [ws.detialView reloadData];
            }
        }
    } FailureBlock:^(id error) {
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
- (void)dealloc {
    
}


@end
